package per.turtle.job.controller;

import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import per.turtle.common.core.web.controller.BaseController;
import per.turtle.common.core.web.entity.CommonResult;
import per.turtle.common.core.web.page.TableDataInfo;
import per.turtle.common.excel.annotation.ResponseExcel;
import per.turtle.common.log.annotation.Log;
import per.turtle.common.log.enums.BusinessType;
import per.turtle.common.security.annotation.PreAuthorize;
import per.turtle.job.domain.SysJobLog;
import per.turtle.job.service.ISysJobLogService;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 调度日志操作处理
 *
 * @author turtle
 */
@RequestMapping("job/log")
@RequiredArgsConstructor
@RestController
public class SysJobLogController extends BaseController {

    private final ISysJobLogService jobLogService;

    @Operation(summary = "清空定时任务调度日志", description = "清空定时任务调度日志")
    @DeleteMapping("clean")
    @Log(title = "调度日志", businessType = BusinessType.CLEAN)
    @PreAuthorize(hasPermission = "monitor:job:remove")
    public CommonResult<Void> clean() {
        jobLogService.cleanJobLog();
        return CommonResult.success();
    }

    @Operation(summary = "导出定时任务调度日志列表", description = "导出定时任务调度日志列表")
    @Log(title = "任务调度日志", businessType = BusinessType.EXPORT)
    @PostMapping("export")
    @PreAuthorize(hasPermission = "monitor:job:export")
    @ResponseExcel("任务调度日志")
    public List<SysJobLog> export(HttpServletResponse response, @RequestBody SysJobLog sysJobLog) {
        return jobLogService.selectJobLogList(sysJobLog);
    }

    @Operation(summary = "根据调度编号获取详细信息", description = "根据调度编号获取详细信息")
    @GetMapping("{configId}")
    @PreAuthorize(hasPermission = "monitor:job:query")
    public CommonResult<SysJobLog> getInfo(@PathVariable Long jobLogId) {
        return CommonResult.success(jobLogService.selectJobLogById(jobLogId));
    }

    @Operation(summary = "查询定时任务调度日志列表", description = "查询定时任务调度日志列表")
    @PostMapping("list")
    @PreAuthorize(hasPermission = "monitor:job:list")
    public TableDataInfo<SysJobLog> list(@RequestBody SysJobLog sysJobLog) {
        List<SysJobLog> list = startPage(s -> jobLogService.selectJobLogList(sysJobLog));
        return getDataTable(list);
    }

    @Operation(summary = "删除定时任务调度日志", description = "删除定时任务调度日志")
    @DeleteMapping("{jobLogIds}")
    @Log(title = "定时任务调度日志", businessType = BusinessType.DELETE)
    @PreAuthorize(hasPermission = "monitor:job:remove")
    public CommonResult<Void> remove(@PathVariable List<Long> jobLogIds) {
        return result(jobLogService.deleteJobLogByIds(jobLogIds));
    }

}
